package cn.tedu._02notice.controller;

import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeAddQuery;
import cn.tedu._02notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._02notice.pojo.entitly.Notice;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._02notice.pojo.vo.NoticeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController//组合注解 @Controller+@ResponseBody
@RequestMapping("/v1/notice")
public class NoticeController {
    @Autowired
    private  NoticeMapper noticeMapper;



    //1.根据API文档确定是否需要VO类和DTO类;
    //2.梳理控制器方法的业务,确定接口;
    //3.完成接口方法和xml配置SQL;
    //4.完成controller中的业务和接口的调用;
    //5.重启工程,测试;

    @PostMapping("/add")

    public String add(NoticeAddParam noticeAddParam){//接受前端传递过来的数据
        System.out.println("noticeAddParam"+noticeAddParam);
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);//将noticeAddParam中的数据拷贝到notice中
        notice.setUserId(100L);//处理其他属性
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        noticeMapper.insert(notice);//调用mapper 插入数据
        return "新增资讯成功";
    }
 @GetMapping("/list")

    public List<NoticeListVO> listVOList(NoticeAddQuery noticeAddQuery){
     System.out.println("noticeAddQuery"+noticeAddQuery);
     String title = noticeAddQuery.getTitle();
     Integer type = noticeAddQuery.getType();
    List<NoticeListVO> noticeListVOS = noticeMapper.list(title, type);
    return noticeListVOS;
 }
    @GetMapping("/detail")

    public NoticeDetailInfoVO detail(Long id){
        return noticeMapper.selectById(id);
    }

    @PostMapping("/del")

    public String delete(Long id) {
        int num = noticeMapper.deleteById(id);
        if (num > 0) {
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

    @PostMapping("/update")

    public String update(NoticeUpdateParam noticeUpdateParam){
        System.out.println("noticeUpdateParam"+noticeUpdateParam);
        Notice notice = new Notice();//将noticeUpdateParam中的数据拷贝到notice中
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdateTime(new Date());
       int num = noticeMapper.update(notice);
       if (num>0){
           return "修改成功";
       }else {
           return "修改失败";
       }
    }
}
